package com.example.inoculations.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.inoculations.entity.Feedback;
import com.example.inoculations.resultR.R;
import com.example.inoculations.service.IFeedbackService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author meirui
 * @since 2022-11-19
 */
@Controller
@ResponseBody
@RequestMapping("/inoculations/feedback")
public class FeedbackController {
    @Autowired
    IFeedbackService iFeedbackService;
    /*分页查询*/
    @GetMapping("/pageall")
    public R<IPage<Feedback>> getpageall(@RequestParam("pageNum") Integer pageNum,
                                         @RequestParam("pageSize") Integer pageSize,
                                         @RequestParam(required = false) String userid) {
        IPage<Feedback> page = new Page<>(pageNum,pageSize);
        QueryWrapper<Feedback> queryWrapper = new QueryWrapper<>();
        /*根据身份证号模糊查询*/
        if (!StringUtils.isEmpty(userid)){
            queryWrapper.like("userid",userid);
        }
        /*根据反馈时间倒序查询*/
//        queryWrapper.orderByDesc("submittime");
        //queryWrapper.orderByDesc("id");//根据id序号倒序查询
        queryWrapper.orderByAsc("backtime");//根据回复时间查询,空的排在最前面
        IPage<Feedback> page1 = iFeedbackService.page(page, queryWrapper);
        return R.SUCCESS(page1);
    }
    /*添加和修改，根据是否传入id，有则修改，无则添加*/
    @PostMapping("/save")
    public R feedbacksave(@RequestBody Feedback feedback){
        if (iFeedbackService.saveOrUpdate(feedback)){
            return R.SUCCESS();
        }else {
            return R.FAIL();
        }
    }
    /*删除*/
    @DeleteMapping("/del/{id}")
    public R feedbackdel(@PathVariable Integer id){
        if (iFeedbackService.removeById(id)){
            return R.SUCCESS();
        }else {
            return R.FAIL();
        }
    }
}
